#include <string.h>
char * trim(char * pstr){

	char * head_nonspac = NULL;
	char * tail_nonspac = NULL;
	int    is_head      = 1;
	int    str_length;
	int move_length;
	int    iterator;

	if( NULL == pstr){
		return NULL;
	}

	str_length = strlen( pstr );
	if( 0 == str_length ){
		return NULL;
	}
	
	for( iterator = 0 ; iterator < str_length ; iterator++ ){

		if( ' ' != pstr[iterator] && is_head ){
			head_nonspac = &pstr[iterator] ;
			is_head = 0;
		}

		if( ' ' != pstr[iterator] && !is_head ){
			tail_nonspac = &pstr[iterator] ;
		}
	}

	/* all is space in the string */
	if( NULL == head_nonspac ){	
		return NULL;
	}

	/* no space in the string */
	move_length = tail_nonspac - head_nonspac + 1;
	if( str_length == move_length ){
		return pstr;
	}

	/* space is only at the tail */
	if( head_nonspac == pstr ){
		*(tail_nonspac+1) = '\0';
		return pstr;
	}

	/* move chars to head */
	for( iterator = 0 ; iterator < move_length ; iterator ++ ){
		pstr[iterator] = head_nonspac[iterator];
	}
	pstr[ move_length ] = '\0';

	return pstr;
	
}
/*
int main(){
	char  test_str [9][9] = {"",	
				"        ",
				"   hello",
				"hello   ",
				"hell   o",
				"  hello ",
				" he llo ",
				"hellohel"};

	int i;
	for (i = 0 ; i<8 ; i++){
		if(!trim(test_str[i])){
			printf("this line is all  space\n");
		}else{
			printf("!%s!\n" , trim(test_str[i]));
		}

	}
}
*/
